//
//  ThemeImageView.m
//  Weibo
//
//  Created by taoshanchang on 15/1/26.
//  Copyright (c) 2015年 taoshanchang. All rights reserved.
//

#import "ThemeImageView.h"
#import "ThemeManager.h"

@implementation ThemeImageView
//用xib创建的话不会调用init方法所以使用awakeFromNib来监听通知
-(void)awakeFromNib{
    [super awakeFromNib];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeNotification:) name:kThemeDidChangeNotification object:nil];
}

-(id)initWithImageName:(NSString *)image{
    self = [self init];
    if (self != nil) {
        self.imageName = image;
    }
    return self;
}

-(id)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeNotification:) name:kThemeDidChangeNotification object:nil];
    }
    
    return self;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangeNotification object:nil];
}

-(void)setImageName:(NSString *)imageName{
    if(_imageName != imageName){
        _imageName = imageName;
    }
    [self loadThemeImage];

}

-(void)loadThemeImage{
    if(self.imageName == nil){
        return ;
    }
    UIImage *image = [[ThemeManager sharedInstance] getThemeImage:self.imageName];
    image = [image stretchableImageWithLeftCapWidth:self.leftCapWidth topCapHeight:self.topCapHeight];
    self.image = image;

}

-(void)themeNotification:(NSNotification *)notification{
    [self loadThemeImage];
}

@end
